package com.dtagri.fjwlw.fragment;

import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.api.ApiHttpClient;
import com.dtagri.fjwlw.base.BaseFragment;
import com.dtagri.fjwlw.bean.TjUsersEntity;
import com.dtagri.fjwlw.ui.MainActivity;
import com.dtagri.fjwlw.ui.ZSimpleBackActivity;
import com.dtagri.fjwlw.ui.ZSimpleBackPage;
import com.dtagri.fjwlw.util.FJwlwConstant;
import com.dtagri.fjwlw.util.TDevice;
import com.dtagri.fjwlw.util.UpdateUtil;
import com.umeng.update.UmengUpdateAgent;

/**
 * 关于
 * 
 * @author wxjn
 * 
 */
public class AboutFragment extends BaseFragment {
	@InjectView(R.id.tv_version_name)
	TextView mTvVersionName;
	private int flag=0;
	private boolean ipflag=false;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.fragment_about, container, false);
		ButterKnife.inject(this, view);
		initView(view);

		initData();
		return view;
	}

	@Override
	public void initView(View view) {
		view.findViewById(R.id.rl_check_update).setOnClickListener(this);
		view.findViewById(R.id.rl_feedback).setOnClickListener(this);
		view.findViewById(R.id.about_icon).setOnClickListener(this);
	}

	@Override
	public void initData() {
		mTvVersionName.setText("V " + TDevice.getVersionName());
	}

	@Override
	public void onClick(View v) {
		final int id = v.getId();
		switch (id) {
		case R.id.rl_check_update:
//			onClickUpdate();
			new UpdateUtil(getActivity(), "others");
			break;
		case R.id.rl_feedback:
			showFeedBack();
			break;
		case R.id.about_icon:
			System.out.println("当前flag"+flag);
			if(flag==0){
				System.out.println("再点击4次");
			}else if (flag==1){
				System.out.println("再点击3次");
			}else if(flag==2){
				System.out.println("再点击2次");
			}else if(flag==3){
				System.out.println("再点击1次");
			}
			else {
				if(ipflag==false&&AppContext.user.getState()==0){
//					System.out.println("点完5次了");
					AppContext.user.setServerIp("27.155.177.209:8081");//27.155.177.209:8081
					AppContext.user.setState(1);
					TjUsersEntity user = AppContext.user;
					
					AppContext.saveUserInfo(user);
					Message msg =new Message();
					msg.arg1 = FJwlwConstant.Handler_flag_ad;
					msg.obj = "27.155.177.209:8081";
					MainActivity.handler.sendMessage(msg);
					
					Toast.makeText(getActivity(), "进入调试模式", Toast.LENGTH_SHORT).show();
					ipflag=true;
					
				}else{
//					System.out.println("点完5次了");
					AppContext.user.setServerIp(ApiHttpClient.IP);//ApiHttpClient.HOST
					AppContext.user.setState(0);
					Message msg =new Message();
					msg.arg1 = FJwlwConstant.Handler_flag_ad;
					msg.obj = ApiHttpClient.IP;
					MainActivity.handler.sendMessage(msg);
					
					Toast.makeText(getActivity(), "服务器改为鼎天农科", Toast.LENGTH_SHORT).show();
					ipflag=false;
				}
			}
			flag=(flag+1)%5;
			break;
		default:
			break;
		}
	}

//	private void onClickUpdate() {
//		UmengUpdateAgent.forceUpdate(getActivity());
//	}

	private void showFeedBack() {
		Intent intent = new Intent(getActivity(), ZSimpleBackActivity.class);
		intent.putExtra(ZSimpleBackActivity.BUNDLE_KEY_PAGE,
				ZSimpleBackPage.FEEDBACK.getValue());
		startActivity(intent);
	}
}
